﻿namespace Raven
{
    public class RocketLauncher : RavenWeapon
    {
        public RocketLauncher(RavenBot owner) : base(WeaponType.type_rocket_launcher,
            Constants.RocketLauncher_DefaultRounds, Constants.RocketLauncher_MaxRoundsCarried,
            Constants.RocketLauncher_FiringFreq,
            Constants.RocketLauncher_IdealRange, Constants.Rocket_MaxSpeed, owner)
        {
            //setup the fuzzy module
            InitializeFuzzyModule();
        }

        protected override void InitializeFuzzyModule()
        {
            _fuzzyModule = new FuzzyModule();
            FuzzyVariable DistToTarget = _fuzzyModule.CreateFLV("DistToTarget");

            FzSet Target_Close = DistToTarget.AddLeftShoulderSet("Target_Close",0,25,150);
            FzSet Target_Medium = DistToTarget.AddTriangularSet("Target_Medium",25,150,300);
            FzSet Target_Far = DistToTarget.AddRightShoulderSet("Target_Far",150,300,1000);

            FuzzyVariable Desirability = _fuzzyModule.CreateFLV("Desirability"); 
            FzSet VeryDesirable = Desirability.AddRightShoulderSet("VeryDesirable", 50, 75, 100);
            FzSet Desirable = Desirability.AddTriangularSet("Desirable", 25, 50, 75);
            FzSet Undesirable = Desirability.AddLeftShoulderSet("Undesirable", 0, 25, 50);

            FuzzyVariable AmmoStatus = _fuzzyModule.CreateFLV("AmmoStatus");
            FzSet Ammo_Loads = AmmoStatus.AddRightShoulderSet("Ammo_Loads", 10, 30, 100);
            FzSet Ammo_Okay = AmmoStatus.AddTriangularSet("Ammo_Okay", 0, 10, 30);
            FzSet Ammo_Low = AmmoStatus.AddTriangularSet("Ammo_Low", 0, 0, 10);


            _fuzzyModule.AddRule(new FzAND(Target_Close, Ammo_Loads), Undesirable);
            _fuzzyModule.AddRule(new FzAND(Target_Close, Ammo_Okay), Undesirable);
            _fuzzyModule.AddRule(new FzAND(Target_Close, Ammo_Low), Undesirable);

            _fuzzyModule.AddRule(new FzAND(Target_Medium, Ammo_Loads), VeryDesirable);
            _fuzzyModule.AddRule(new FzAND(Target_Medium, Ammo_Okay), VeryDesirable);
            _fuzzyModule.AddRule(new FzAND(Target_Medium, Ammo_Low), Desirable);

            _fuzzyModule.AddRule(new FzAND(Target_Far, Ammo_Loads), Desirable);
            _fuzzyModule.AddRule(new FzAND(Target_Far, Ammo_Okay), Undesirable);
            _fuzzyModule.AddRule(new FzAND(Target_Far, Ammo_Low), Undesirable);
        }

        public override void Destroy()
        {
        }

        public override void ShootAt(Vector2 pos)
        {
            if (NumRoundsRemaining() > 0 && isReadyForNextShot())
            {
                //fire off a rocket!
                _owner.GetWorld().AddRocket(_owner, pos);

                DecrementNumRounds();

                UpdateTimeWeaponIsNextAvailable();

                //add a trigger to the game so that the other bots can hear this shot
                //(provided they are within range)
                _owner.GetWorld().GetMap().AddSoundTrigger(_owner, Constants.RocketLauncher_SoundRange);
            }
        }

        public override Fix GetDesirability(Fix DistToTarget)
        {
            if (_numRoundsLeft == 0)
            {
                _lastDesirabilityScore = 0;
            }
            else
            {
                // //fuzzify distance and amount of ammo
                _fuzzyModule.Fuzzify("DistToTarget", DistToTarget);
                _fuzzyModule.Fuzzify("AmmoStatus", _numRoundsLeft);
                
                _lastDesirabilityScore = _fuzzyModule.DeFuzzify("Desirability");
            }

            return _lastDesirabilityScore;
        }
    }
}